home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Car / Battery.m < prev    next >
Encoding:
Text File  |  1992-06-25  |  4.0 KB  |  217 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Car_main.h"
  5. #import "Battery.h"
  6. #import "DataView.h"
  7. #import <appkit/nextstd.h>
  8.  
  9. @implementation Battery
  10.  
  11. - init
  12. {
  13.     [super init];
  14.     battery = self;
  15.     return self;
  16. }
  17.  
  18. - read:(NXTypedStream *)stream
  19. {
  20.     [super read:stream];
  21.     NXReadTypes(stream,"ffffcccd",&efficiency,&mass,&max,&peakDraw,&plotAmps,&plotCharge,&plotVolts,&present);
  22.     NXReadArray(stream,"f",6,ampCoefficients);
  23.     NXReadArray(stream,"f",6,voltCoefficients);
  24.     return self;
  25. }
  26.  
  27. - write:(NXTypedStream *)stream
  28. {
  29.     [super write:stream];
  30.     NXWriteTypes(stream,"ffffcccd",&efficiency,&mass,&max,&peakDraw,&plotAmps,&plotCharge,&plotVolts,&present);
  31.     NXWriteArray(stream,"f",6,ampCoefficients);
  32.     NXWriteArray(stream,"f",6,voltCoefficients);
  33.     return self;
  34. }
  35.  
  36. - (float *)ampCoefficients
  37. {
  38.     return ampCoefficients;
  39. }
  40.  
  41. - setAmpCoefficients:(float *)theCoefficients
  42. {
  43. int i;
  44.  
  45.     for ( i = 0 ; i < 6 ; i++ )
  46.         ampCoefficients[i] = theCoefficients[i];
  47.     return self;
  48. }
  49.  
  50. - (float)efficiency
  51. {
  52.     return efficiency;
  53. }
  54.  
  55. - setEfficiency:(float)aNumber
  56. {
  57.     efficiency = aNumber;
  58.     return self;
  59. }
  60.  
  61. - (float)mass
  62. {
  63.     return mass;
  64. }
  65.  
  66. - setMass:(float)aNumber
  67. {
  68.     mass = aNumber;
  69.     return self;
  70. }
  71.  
  72. - (float)max
  73. {
  74.     return max;
  75. }
  76.  
  77. - setMax:(float)aNumber
  78. {
  79.     max = aNumber;
  80.     return self;
  81. }
  82.  
  83. - (float)peakDraw
  84. {
  85.     return peakDraw;
  86. }
  87.  
  88. - setPeakDraw:(float)aNumber
  89. {
  90.     peakDraw = aNumber;
  91.     return self;
  92. }
  93.  
  94. - (BOOL)plotAmps
  95. {
  96.     return plotAmps;
  97. }
  98.  
  99. - setPlotAmps:(BOOL)flag
  100. {
  101.     plotAmps = flag;
  102.     return self;
  103. }
  104.  
  105. - (BOOL)plotCharge
  106. {
  107.     return plotCharge;
  108. }
  109.  
  110. - setPlotCharge:(BOOL)flag
  111. {
  112.     plotCharge = flag;
  113.     return self;
  114. }
  115.  
  116. - (BOOL)plotVolts
  117. {
  118.     return plotVolts;
  119. }
  120.  
  121. - setPlotVolts:(BOOL)flag
  122. {
  123.     plotVolts = flag;
  124.     return self;
  125. }
  126.  
  127. - (double)present
  128. {
  129.     return present;
  130. }
  131.  
  132. - setPresent:(double)aNumber
  133. {
  134.     present = aNumber;
  135.     return self;
  136. }
  137.  
  138. - (float *)voltCoefficients
  139. {
  140.     return voltCoefficients;
  141. }
  142.  
  143. - setVoltCoefficients:(float *)theCoefficients
  144. {
  145. int i;
  146.  
  147.     for ( i = 0 ; i < 6 ; i++ )
  148.         voltCoefficients[i] = theCoefficients[i];
  149.     return self;
  150. }
  151.  
  152. /******************************************************************************************************************************
  153.  *    This is the actual part that does the work.  It's not very complicated.  Note: It's a lot better to have very separate  *
  154.  *    cases for positive, zero, and negative power.  The code is less efficient, but there is much less confusion this way.   *
  155.  *    Eventually, the other objects should be rewritten in this fashion.  Sorry I didn't think of this earlier.               *
  156.  ******************************************************************************************************************************/
  157. - (float)powerRequested:(float)power forTime:(float)time
  158. {
  159. float volts;
  160. float amps;
  161. float ampsAvailable;
  162.  
  163.     if ( power > 0 )
  164.         {
  165.         volts = evaluatePolynomial(voltCoefficients,6,present);        // Get the maximum voltage available
  166.         ampsAvailable = evaluatePolynomial(ampCoefficients,6,present);    // Get the maximum curretn available
  167.  
  168.         amps = power / volts;                        // Calculate the maximum current needed
  169.         amps = MIN(amps,ampsAvailable);                    // Take to lower one
  170.  
  171.         present -= ( amps * volts * time / efficiency ) / 3600000.0;    // Update the current charge level
  172.  
  173.         return (amps * volts);
  174.         }
  175.     else if ( power < 0 )
  176.         {
  177.         present -= ( power * time * efficiency ) / 3600000;        // Since the power is negative, we multiply by the 
  178.         return power;                            // Battery efficiency so that we recharge less than we
  179.         }                                // get. Remember to convert J to kWh!
  180.     else if ( power == 0 )
  181.         {
  182.         return 0;
  183.         }
  184.     return 0;
  185. }
  186.  
  187. - getSetForRun
  188. {
  189.     startingLevel = present;
  190.     return self;
  191. }
  192.  
  193. - finishedRun
  194. {
  195.     present = startingLevel;
  196.     return self;
  197. }
  198.  
  199. - report:(NXStream *)stream
  200. {
  201.     NXPrintf(stream,"Battery:\n");
  202.     NXPrintf(stream,"  Energy Used:%fkWh\n",startingLevel-present);
  203.     return self;
  204. }
  205.  
  206. @end
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.